#!/usr/bin/env python
from Notifier import Notifier
from DGSNotifier import DGSNotifier
#import LGNotifier
from Growl import GrowlNotifier
from time import sleep
import ConfigParser

notifiers = []
growl = None

def registerWithGrowl(notifiers):
    global growl
    app_name = "Your Move!"
    alerts = []
    for notifier in notifiers:
        alerts.append(notifier.getName() + ": Game Move")
        alerts.append(notifier.getName() + ": Message Received")
    growl = GrowlNotifier(app_name, alerts)
    growl.register()

def notifyGame(game):
    notification = game.getNotifier().getName() + ": Game Move"
    notify_title = game.getNotifier().getName()
    notify_message = game.getOpponentName() + " played a move in your game on " + game.getLastMove() + "."
    growl.notify(notification, notify_title, notify_message,game.getIcon(),False)

def mainEventLoop():
    # TODO: Allow each server to have its own time cycle
    timeCycle =  notifiers[0].getRefreshInterval()
    while True:
        print "Checking for game!"
        for n in notifiers:
            for game in n.checkForGames():
                notifyGame(game)
        sleep(timeCycle)
        

if __name__ == '__main__':
    config = ConfigParser.ConfigParser()
    config.read('../your-move.conf')
    print config.items('Global')
    notifiers.append(DGSNotifier("AdrianP"))
    notifiers[0].setRefreshInterval(60)
    registerWithGrowl(notifiers)
    mainEventLoop()
    